package com.cloud.manager.web.controller;

import com.cloud.manager.core.config.CloudConfig;
import com.cloud.manager.core.exception.ServiceException;
import com.cloud.manager.core.result.Result;
import com.cloud.manager.core.result.ResultCreator;
import com.cloud.manager.utils.StringUtils;
import com.cloud.manager.utils.parser.ParserClient;
import com.cloud.manager.web.body.LoginBody;
import com.cloud.manager.web.domain.App;
import com.cloud.manager.web.domain.AppInfo;
import com.cloud.manager.web.domain.User;
import com.cloud.manager.web.domain.Version;
import com.cloud.manager.web.service.AppService;
import com.cloud.manager.web.service.VersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 描述：
 *
 * @author FineEx   2021/11/30
 * @version 1.0
 */
@Api(tags = "APP管理")
@CrossOrigin
@RestController
@RequestMapping("/app")
public class AppController {
    @Resource
    private AppService appService;
    @Resource
    private VersionService versionService;

    @ApiOperation(value = "App列表")
    @GetMapping("/list")
    public Result<List<App>> appList() {
        List<App> apps = appService.selectAllApp();
        return ResultCreator.success(apps);
    }

    @ApiOperation(value = "App版本列表")
    @GetMapping("/versionList/{appId}")
    public Result<List<Version>> appVersionList(@PathVariable Long appId) {
        if (appId < 0) {
            throw new ServiceException("参数错误！");
        }
        List<Version> versions = versionService.selectVersionList(appId);
        return ResultCreator.success(versions);
    }

    @ApiOperation(value = "删除版本")
    @GetMapping("/deleteVersion/{versionId}")
    public Result<Integer> deleteVersion(@PathVariable Long versionId) {
        if (versionId < 0) {
            throw new ServiceException("参数错误！");
        }
        Integer deleteVersion = versionService.deleteVersion(versionId);
        return ResultCreator.success(deleteVersion);
    }

    @ApiOperation(value = "新增APP版本")
    @PostMapping("/addApp")
    public Result<String> addApp(@RequestParam("file") MultipartFile file, @RequestBody App app) {
        if (file == null || file.isEmpty()) {
            return ResultCreator.fail("上传失败，文件内容为空！");
        }
        String fileName = file.getOriginalFilename();
        File localFile = new File(CloudConfig.getUploadPath() + File.separator + fileName);
        if (!localFile.getParentFile().exists()) {
            localFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(localFile);
            AppInfo appInfo = ParserClient.parse(localFile.getPath());
            appService.addApp(appInfo);
            return ResultCreator.success();
        } catch (IOException e) {
            return ResultCreator.fail("文件上传失败，绝对路径：{}" + localFile.getPath());
        } catch (ClassNotFoundException e) {
            return ResultCreator.fail("文件解析失败！");
        }
    }

    @ApiOperation(value = "解析APP")
    @PostMapping("/parseApp")
    public Result<App> parseApp(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return ResultCreator.fail("上传失败，文件内容为空！");
        }
        String fileName = file.getOriginalFilename();
        File localFile = new File(CloudConfig.getUploadPath() + File.separator + fileName);
        if (!localFile.getParentFile().exists()) {
            localFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(localFile);
            AppInfo appInfo = ParserClient.parse(localFile.getPath());
            FileUtils.delete(localFile);
            return ResultCreator.success(appInfo.getApp());
        } catch (IOException | ClassNotFoundException e) {
            return ResultCreator.fail("文件解析失败！");
        }
    }
}
